//
//  ViewController.m
//  09-UITableViewTest
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#define kTableViewReuse @"reuseIndentifier"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *nameArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *arr = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.nameArr = [NSMutableArray arrayWithArray:arr];
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.view addSubview:_tableView];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
// 删除按钮的标题
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
// 可以代替tableView: commitEditingStyle: forRowAtIndexPath:方法实现功能≤
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        id temp = [[_nameArr objectAtIndex:indexPath.row] copy];
        [_nameArr removeObjectAtIndex:indexPath.row];
        [_nameArr insertObject:temp atIndex:0];
        [_tableView reloadData];
    }];
    UITableViewRowAction *rowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [_nameArr removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }];
    return @[rowAction, rowAction1];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kTableViewReuse];
    }
    cell.textLabel.text = _nameArr[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
